package io.renren.modules.project.service.impl;

import io.renren.common.utils.RandomId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.modules.project.dao.ProjectPaymentDao;
import io.renren.modules.project.entity.ProjectPaymentEntity;
import io.renren.modules.project.service.ProjectPaymentService;



@Service("projectPaymentService")
public class ProjectPaymentServiceImpl implements ProjectPaymentService {
	@Autowired
	private ProjectPaymentDao projectPaymentDao;
	
	@Override
	public ProjectPaymentEntity queryObject(String id){
		return projectPaymentDao.queryObject(id);
	}
	
	@Override
	public List<ProjectPaymentEntity> queryList(Map<String, Object> map){

		return projectPaymentDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return projectPaymentDao.queryTotal(map);
	}
	
	@Override
	public void save(ProjectPaymentEntity projectPayment){
		projectPayment.setId(RandomId.createId()+"");
		//我要获取当前的日期
		Date date = new Date();
		//设置要获取到什么样的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取String类型的时间
		String createdate = sdf.format(date);
		projectPayment.setCreationTime(createdate);
		//四舍五入改变金额，保留两位小数点存进去
		projectPayment.setPaymentAmountStr(projectPayment.getPaymentAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
		projectPaymentDao.save(projectPayment);
	}
	
	@Override
	public void update(ProjectPaymentEntity projectPayment){
//我要获取当前的日期
		Date date = new Date();
		//设置要获取到什么样的时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取String类型的时间
		String createdate = sdf.format(date);
		projectPayment.setCreationTime(createdate);
		//四舍五入改变金额，保留两位小数点存进去
		projectPayment.setPaymentAmountStr(projectPayment.getPaymentAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
		projectPaymentDao.update(projectPayment);
	}
	
	@Override
	public void delete(String id){
		projectPaymentDao.delete(id);
	}
	
	@Override
	public void deleteBatch(String[] ids){
		projectPaymentDao.deleteBatch(ids);
	}

	@Override
	public ProjectPaymentEntity queryPaymenAmount(String projectId) {
		ProjectPaymentEntity projectPaymentEntity = new ProjectPaymentEntity();
		List<ProjectPaymentEntity> queryPaymenAmount = projectPaymentDao.queryPaymenAmount(projectId);
		if (queryPaymenAmount != null && queryPaymenAmount.size()>0){
			for (int i = 0 ; i < queryPaymenAmount.size(); i++){
				if (queryPaymenAmount.get(i).getPaymentType() != null && "1".equals(queryPaymenAmount.get(i).getPaymentType())){
					projectPaymentEntity.setPaymentSum(queryPaymenAmount.get(i).getPaymentAmountStr());
				}
				if (queryPaymenAmount.get(i).getPaymentType() != null && "0".equals(queryPaymenAmount.get(i).getPaymentType())){
					projectPaymentEntity.setReceivablesSum(queryPaymenAmount.get(i).getPaymentAmountStr());
				}
			}
		}
		return projectPaymentEntity;
	}

}
